//
//  HMStatusOriginalView.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/14.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit

class HMStatusOriginalView: UIView {

    
    
    var status: HMStatus?{
        didSet{
            
            //cell将模型原创view,
            
            if let profile_image_url = status?.user?.profile_image_url{
            let url = NSURL(string: profile_image_url)
                iconView.sd_setImageWithURL(url,placeholderImage: UIImage(named: "avatar"))
            
            }
            
            //用户名称
            nameLabel.text = status?.user?.screen_name
            
            //时间
            timeLabel.text = status?.created_at
            
            //来源
            sourceLabel.text = status?.source
            
            //微博内容
            contentLabel.text = status?.text
            
            verifiedView.image = status?.user?.verifiedView
            

            memberView.image = status?.user?.mbrankImage

            
         pictureView.pictureURLs = status?.storePicutreURLs
            
//            pictureView.snp_updateConstraints { (make) in
//                let margin = status?.storePicutreURLs == nil ? 0 : HMStatusCellMargin
//                make.top.equalTo(contentLabel.snp_bottom).offset(margin)
//            }
            
            
            
            //设置用户的会员等级
//            if let mbrank = status?.user?.mbrank{
//                if mbrank > 0 && mbrank <= 6 {
//                    //设置会员图片
//                    memberView.image = UIImage(named: "common_icon_membership_level\(mbrank)")
//                }else{
//                
//                    memberView.image = nil
//                }
//            }
//            
            //认证类型
//            if let verified_type = status?.user?.verified_type{
//            
//                switch verified_type {
//                case 0:
//                    verifiedView.image = UIImage(named: "avatar_vip")
//                    case 2,3,5:
//                    verifiedView.image = UIImage(named: "avatar_enterprise_vip")
//                    case 220:
//                        verifiedView.image = UIImage(named: "avatar_grassroot")
//
//
//                default:
//                    verifiedView.image = nil
//                    
//                }
//            
//            
//            }
            
        }}
    override init(frame: CGRect) {
        super.init(frame: frame)

    
    
        //定义view的颜色
        backgroundColor = UIColor.whiteColor()
        
        //添加控件
        addSubview(iconView)
        addSubview(nameLabel)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(memberView)
        addSubview(verifiedView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //设置约束
        
        iconView.snp_makeConstraints { (make) in
            make.leading.top.equalTo(self).offset(HMStatusCellMargin)
            make.size.equalTo(CGSizeMake(HMStatusIconWH, HMStatusIconWH))
        }

        //名称
        
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView)
            make.leading.equalTo(iconView.snp_trailing).offset(HMStatusCellMargin)
        }
        
        // 时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView)
            make.leading.equalTo(nameLabel)
        }
        
        // 来源
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(timeLabel.snp_trailing).offset(HMStatusCellMargin)
            make.centerY.equalTo(timeLabel)
        }
        
        // 会员等级
        memberView.snp_makeConstraints { (make) -> Void in
            make.centerY.equalTo(nameLabel)
            make.leading.equalTo(nameLabel.snp_trailing).offset(HMStatusCellMargin)
            make.size.equalTo(CGSize(width: 14, height: 14))
        }
        
        // 认证图标
        verifiedView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_trailing).offset(-8)
            make.centerY.equalTo(iconView.snp_bottom).offset(-8)
            make.size.equalTo(CGSize(width: 17, height: 17))
        }
        
        // 微博内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self).offset(HMStatusCellMargin)
            make.top.equalTo(iconView.snp_bottom).offset(HMStatusCellMargin)
            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
        }
        
        pictureView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            make.leading.equalTo(self).offset(HMStatusCellMargin)
            //            make.trailing.equalTo(self).offset(-HMStatusCellMargin)
            
            // TODO: 测试高度先写死
            //            make.height.equalTo(Int(arc4random_uniform(200)) + 100)
            make.size.equalTo(CGSizeZero)
        }
        
        /// 自定义的原创微博view的底部和contentLabel的底部重合,偏移8
        self.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(pictureView).offset(HMStatusCellMargin)
        }
    }

    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    //懒加载控件
   //头像
    private lazy var iconView: UIImageView = {
    
        let imageView = UIImageView.init(image: UIImage(named: "avatar"))
        //头像弄成圆的
        imageView.layer.cornerRadius = 17.5
        
        imageView.layer.masksToBounds = true
        
        return imageView
    
    }()
    
    //用户名称Label
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(HMStatusNameFontSize)
        label.textColor = UIColor.darkGrayColor()
        return label
        
    }()
    
    //时间Label
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(HMStatusTimeFontSize)
        label.textColor = UIColor.orangeColor()
        return label
        
    }()
    
    private lazy var sourceLabel : UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(HMStatusTimeFontSize)
        label.textColor = UIColor.lightGrayColor()
        return label
        
    }()
    
    //会员图标
    private lazy var memberView = UIImageView.init(image: UIImage(named:"common_icon_membership_expired"))
    
    //认证图标
    
    private lazy var verifiedView = UIImageView.init(image: UIImage(named:"avatar_vip"))
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(HMStatusContentFontSize)
        label.textColor = UIColor.darkGrayColor()
        
        //设置文字不一样,方便测试
        
        let result = arc4random_uniform(100)%2
        
        label.text = result == 0 ? "啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得" : "啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得啊索得"
        //自动匹配大小
        label .sizeToFit()
        //设置换行
        label.numberOfLines = 0
        return label
    }()

    private lazy var pictureView: HMStatuPictureView  = HMStatuPictureView()
}
